'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0102
    Inherits Snac

    Public Sub New()
        MyBase.New(&H1, &H2)
    End Sub

    Private _Families As New List(Of FamilyIdToolPair)

    Public ReadOnly Property Families() As List(Of FamilyIdToolPair)
        Get
            Return _Families
        End Get
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _Families.Count * FamilyIdToolPair.SizeFixPart
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        For Each x As FamilyIdToolPair In _Families
            data.AddRange(x.Serialize)
        Next

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Do While index < data.Count
            Dim fam As New FamilyIdToolPair

            fam.Deserialize(data.GetRange(index, 8))

            _Families.Add(fam)

            index += FamilyIdToolPair.SizeFixPart
        Loop

        SetTotalSize(index)
    End Sub
End Class

Public Class FamilyIdToolPair
    Implements ISerializable

    Public Const SizeFixPart As Integer = 8

    Public Sub New()

    End Sub

    Public Sub New(ByVal serviceId As Integer, ByVal version As Integer, ByVal toolId As Integer, ByVal toolVersion As Integer)
        _FamilyNumber = serviceId
        _FamilyVersion = version
        _ToolId = toolId
        _ToolVersion = toolVersion
    End Sub

    Private _FamilyNumber As Integer
    Public Property FamilyNumber() As Integer
        Get
            Return _FamilyNumber
        End Get
        Set(ByVal value As Integer)
            _FamilyNumber = value
        End Set
    End Property

    Private _FamilyVersion As Integer
    Public Property FamilyVersion() As Integer
        Get
            Return _FamilyVersion
        End Get
        Set(ByVal value As Integer)
            _FamilyVersion = value
        End Set
    End Property

    Private _ToolId As Integer
    Public Property ToolId() As Integer
        Get
            Return _ToolId
        End Get
        Set(ByVal value As Integer)
            _ToolId = value
        End Set
    End Property

    Private _ToolVersion As Integer
    Public Property ToolVersion() As Integer
        Get
            Return _ToolVersion
        End Get
        Set(ByVal value As Integer)
            _ToolVersion = value
        End Set
    End Property

    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return 0
        End Get
    End Property

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return SizeFixPart
        End Get
    End Property

    Public Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize
        Return 0
    End Function

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return SizeFixPart
    End Function

    Public Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        _FamilyNumber = ByteConverter.ToUInt16(data.GetRange(0, 2))
        _FamilyVersion = ByteConverter.ToUInt16(data.GetRange(2, 2))
        _ToolId = ByteConverter.ToUInt16(data.GetRange(4, 2))
        _ToolVersion = ByteConverter.ToUInt16(data.GetRange(6, 2))

        _HasData = True
    End Sub

    Public Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Dim data As List(Of Byte)

        data = New List(Of Byte)

        data.AddRange(ByteConverter.GetBytes(CUShort(_FamilyNumber)))
        data.AddRange(ByteConverter.GetBytes(CUShort(_FamilyVersion)))
        data.AddRange(ByteConverter.GetBytes(CUShort(_ToolId)))
        data.AddRange(ByteConverter.GetBytes(CUShort(_ToolVersion)))

        Return data
    End Function

    Private _HasData As Boolean
    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

End Class